<template>
  <div class="">
   <Header title="地址编辑"></Header>
   <van-address-edit
      :address-info="data.addressInfo"
      :area-list="data.areaList"
      show-postal
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
      @change-detail="onChangeDetail"
    />
  </div>
</template>

<script  setup>
import { Toast } from 'vant';
import Header from '../../components/Header.vue';
import { onMounted, reactive } from 'vue';
import { useRoute,useRouter } from 'vue-router';
import { useStore } from 'vuex';
let store=useStore()
let route=useRoute()
let router=useRouter()
let data = reactive({
      areaList: {
        // 110000：省，加100：市，加1：区
        province_list: {
          110000: "黑龙江省",
          120000: "辽宁省",
        },
        city_list: {
          110100: "哈尔滨市",
          110200: "大庆市",
          120100: "沈阳市",
          120200: "大连市",
        },
        county_list: {
          110101: "南岗区",
          110102: "香坊区",
          120102: "铁西区",
          120202: "旅顺口区",
        },
      },
      addressInfo:{}
    });
  const init=()=>{
   store.state.Cart.userAddress.forEach(item => {
    if(item.id===Number(route.query.id))
  {
   return data.addressInfo=item
  }

  });
  
}

onMounted(()=>{
  init()
})

const onSave=(content)=>{
  if(route.query.type=='add'){ 
    if(store.state.Cart.userAddress.length)
       {
          content.id=store.state.Cart.userAddress[store.state.Cart.userAddress.length-1].id+1
       }
       else{
        content.id=1001
       }
      store.commit('onaddress',content)
      Toast('保存成功')
      setTimeout(() => {
     router.back()
  }, 1000);
  }
  else{
  store.commit('change',content)
  Toast('保存成功')
  setTimeout(() => {
     router.back()
  }, 1000);  
  }
  
}

const onDelete=(content)=>{
  store.commit('deleteaddress',content)
  Toast('删除成功')
  setTimeout(() => {
     router.back()
  }, 1000);

}

</script>

<style scoped>
/deep/.van-switch--on{
  background: #ffc400;
}

/deep/.van-button--danger{
  background: #ffc400;
  border: none;
}
</style>
